﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NReader.Common;
using NReader.Service;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace NReader
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class Chapter : NReader.Common.LayoutAwarePage
    {
        public Chapter()
        {
            this.InitializeComponent();
        }

        private NReader.Service.RAS.ChapterInfo _chapter;

        private IList<NReader.Service.RAS.ChapterInfo> _chapters;

        private IList<NReader.Service.RAS.ChapterInfo> _maxChapters;

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            var tranInfo = navigationParameter as BookTranChapterInfo;
            if (tranInfo != null)
            {
                _chapter = tranInfo.CurrentChapter;
                _maxChapters = tranInfo.MaxChapters;
                this.DefaultViewModel["Chapter"] = _chapter;
                //wvContent.NavigateToString("<html><body style=\"background-color: yellowgreen;\"><div >asdasdasdasdasdasdasd</div></body></html>");
                //wvContent.Navigate(new Uri(_chapter.chapterlink));
            }
            if (_chapters == null && _chapter != null)
            {
                var service = new ArticleService();
                var list = await service.GetBookReader(_chapter.articleid, _chapter.webflag);
                this.DefaultViewModel["Chapters"] = list;
            }
            if (_maxChapters != null)
            {
                this.DefaultViewModel["MaxChapters"] = _maxChapters;
            }
        }

        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {

        }

        private void LvBookReaderItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private async void BtnBookReaderClick(object sender, RoutedEventArgs e)
        {
            if (!ppBookReader.IsOpen)
            {
                spBookReader.Width = 400;
                spBookReader.Height = Window.Current.Bounds.Height;
                ppBookReader.HorizontalOffset = Window.Current.Bounds.Width - 400;
                ppBookReader.IsOpen = true;
            }
        }

        private void PpBookReaderPopupClosed(object sender, object e)
        {
            //wvContent.Visibility = Windows.UI.Xaml.Visibility.Visible;
            //rcContent.Fill = new SolidColorBrush(Windows.UI.Colors.Transparent);
        }

        private void PpBookReaderPopupOpened(object sender, object e)
        {
            //if (rcContent.Visibility == Windows.UI.Xaml.Visibility.Visible)
            //{
            //    var b = new WebViewBrush();
            //    b.SourceName = "wvContent";
            //    b.Redraw();
            //    rcContent.Fill = b;
            //    wvContent.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            //}
        }

        private void PpSettingClosed(object sender, object e)
        {

        }

        private void PpSettingOpened(object sender, object e)
        {

        }

        private void BtnSettingClick(object sender, RoutedEventArgs e)
        {
            if (!ppSetting.IsOpen)
            {
                spSetting.Width = 400;
                spSetting.Height = Window.Current.Bounds.Height;
                ppSetting.HorizontalOffset = Window.Current.Bounds.Width - 400;
                ppSetting.IsOpen = true;
            }
        }
    }
}
